﻿using System;
using System.Web;
using System.Data;

/// <summary>
/// 每天处理：理财产品和借款等，包括利息、该还借款等
/// predealdate和
/// curdealdate应放在数据库中
/// 表名暂定：SF_everydaydeal
/// </summary>
public static class Everydaydeal
{
    /// <summary>
    /// 最后已处理日期的前一个已处理日期
    /// </summary>
    public static int predealdate = 20010101;
    /// <summary>
    /// 最后已处理日期
    /// </summary>
    public static int curdealdate = 20010101;
    /// <summary>
    /// 每天处理：理财产品和借款等，包括利息、该还借款等
    /// </summary>
    /// <param name="URL"></param>
    /// <returns></returns>
    public static void deal_tzAndloan()
    {
        int getdatenow = Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd"));
        if (getdatenow == curdealdate)
        {
            return;
        }
        if (getdatenow > curdealdate)
        {
           
            
                //处理理财产品,包括利息、该还借款等


                //处理借款,包括利息、该还借款等


                predealdate = curdealdate;
                curdealdate = getdatenow;
           
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="returnType">还款方式</param>
    /// <param name="money">借款金额</param>
    /// <param name="month">借款期数</param>
    /// <param name="yearRate">年利率</param>
    /// <returns></returns>
    public static DataTable CreateReturnPlan(int returnType, decimal money, int month, decimal yearRate)
    {
        yearRate = yearRate / 100 / 12;
        DataTable dt = new DataTable();
        dt.Columns.Add("monthpayper");//借出金额（月还利息）
        dt.Columns.Add("monthpay");//月还本金
        dt.Columns.Add("monthpaytotal");//已还款金额（月还本息）
        dt.Columns.Add("nopay");//未还款金额（本息余额）

        decimal C, D, I, B, totalAmt;
        if (returnType == 1)//等额本息
        { 
            C = money * yearRate * Power(1 + yearRate, month) / (Power(1 + yearRate, month) - 1);
            for (int i = 0; i < month; i++) {//D:剩余本金//I:剩余本金产生的利息
                // I = 剩余本金 × 月利率
                D = i == 0 ? money : Convert.ToDecimal(dt.Rows[i - 1][3]); //剩余本金
                I = D * yearRate;
                DataRow dr=dt.NewRow(); 
                dr[0]=I;  
                dr[1]=C - I;  
                dr[2]=C;  
                dr[3]=D - (C - I);  
                dt.Rows.Add(dr);                      
            }
        }
        else if (returnType == 2)//等额本金
        {

            B = money / month;
            for (int i = 0; i < month; i++)
            {
                D = i == 0 ? money : Convert.ToDecimal(dt.Rows[i - 1][3]); 
                I = D * yearRate; 
                DataRow dr = dt.NewRow();  
                dr[0] = I;
                dr[1] = B;
                dr[2] = B + I;
                dr[3] = D - B;
                dt.Rows.Add(dr);
            }

        }
        else if (returnType == 3)//还本付息
        {
            //每月付息//B月还本金 
            I = money * yearRate; //月利息
            for (int i = 0; i < month ; i++) {
                if (i < month-1) {

                    B = 0; //每月本金 
                    totalAmt = money + I * month;//偿还总本息
                    D = totalAmt - I * (i + 1);  //本息余额
                }
                else {

                    B = money; //最后月还所有本金                             
                    D =0;  //本息余额
                 }
                //月还利息，月还本金，月还本息，本息余额
                DataRow dr = dt.NewRow();  
                dr[0] = I;
                dr[1] = B;
                dr[2] = B + I;
                dr[3] = D ;
                dt.Rows.Add(dr);
            }
                
        }
        return dt;
    }

    public static decimal Power(decimal x, int y)
    {
        decimal rtn=x;
        for (int n = y-1; n > 0; n--)
        {
            rtn = rtn * x;
        }
        return rtn;
    }
         




}